package com.wish233.wiliwilivideo.controller;

import com.wish233.wiliwilicommon.common.BaseResponse;
import com.wish233.wiliwilicommon.common.ErrorCode;
import com.wish233.wiliwilicommon.common.ResultUtils;
import com.wish233.wiliwilicommon.exception.BusinessException;
import com.wish233.wiliwilicommon.exception.ThrowUtils;
import com.wish233.wiliwilivideo.domain.dto.history.CreateOrUpdateHistory;
import com.wish233.wiliwilivideo.domain.vo.VideoViewHistoryVO;
import com.wish233.wiliwilivideo.service.VideoViewHistoryService;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WHH
 */
@RestController
@RequestMapping("/history")
public class HistoryController {

    @Resource
    private VideoViewHistoryService videoViewHistoryService;

    @ApiOperation("添加或更新一段历史记录")
    @PostMapping("/addOrUpdate")
    private BaseResponse<Boolean> addOrUpdateHistory(CreateOrUpdateHistory createOrUpdateHistory) {
        ThrowUtils.throwIfParamNull(createOrUpdateHistory);
        return ResultUtils.success(videoViewHistoryService.addOrUpdateHistory(createOrUpdateHistory));
    }

    @ApiOperation("获取某个视频的观看时长(只支持最近2天)")
    @GetMapping("/playTime")
    private BaseResponse<Long> getVideoViewTime(@RequestParam(value = "videoId") Long videoId) {
        ThrowUtils.throwIfParamNull(videoId);
        return ResultUtils.success(videoViewHistoryService.getVideoViewTime(videoId));
    }

    @ApiOperation("分页查历史记录")
    @GetMapping("/list")
    private BaseResponse<List<VideoViewHistoryVO>> listVideoViewVO(
            @RequestParam("userId") Long userId,
            @RequestParam("current") int current,
            @RequestParam("pageSize") int pageSize
    ) {
        ThrowUtils.throwIfParamNull(userId);
        ThrowUtils.throwIf(current < 1 || pageSize < 1, new BusinessException(ErrorCode.PARAMS_ERROR,"current或pageSize错误"));
        return ResultUtils.success(videoViewHistoryService.listVideoViewVO(userId, current, pageSize));
    }
}
